/*     */ package com.zimbra.cs.octosync.store;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.octosync.PatchManifest;
/*     */ import com.zimbra.cs.store.IncomingBlob;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.OutputStream;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class PatchStore
/*     */ {
/*     */   private BlobStore blobStore;
/*     */   
/*     */   public class IncomingPatch
/*     */   {
/*     */     private IncomingBlob patchBlob;
/*     */     private String accountId;
/*     */     private PatchManifest manifest;
/*     */     
/*     */     public String getAccountId()
/*     */     {
/*  53 */       return this.accountId;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     public IncomingPatch(IncomingBlob blob, String accountId)
/*     */     {
/*  64 */       this.patchBlob = blob;
/*  65 */       this.accountId = accountId;
/*  66 */       this.manifest = new PatchManifest();
/*     */     }
/*     */     
/*     */     public PatchManifest getManifest()
/*     */     {
/*  71 */       return this.manifest;
/*     */     }
/*     */     
/*     */     public OutputStream getOutputStream() throws IOException
/*     */     {
/*  76 */       return this.patchBlob.getAppendingOutputStream();
/*     */     }
/*     */     
/*     */     public InputStream getInputStream() throws IOException
/*     */     {
/*  81 */       return this.patchBlob.getInputStream();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public class StoredPatch
/*     */   {
/*     */     private BlobStore.StoredBlob patchBlob;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */     private BlobStore.StoredBlob manifestBlob;
/*     */     
/*     */ 
/*     */ 
/*     */     private String accountId;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */     public String getAccountId()
/*     */     {
/* 107 */       return this.accountId;
/*     */     }
/*     */     
/*     */     private StoredPatch(BlobStore.StoredBlob patchBlob, BlobStore.StoredBlob manifestBlob, String accountId)
/*     */     {
/* 112 */       this.patchBlob = patchBlob;
/* 113 */       this.manifestBlob = manifestBlob;
/* 114 */       this.accountId = accountId;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     public InputStream getInputStream()
/*     */       throws IOException
/*     */     {
/* 125 */       return this.patchBlob.getInputStream();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     public InputStream getManifestInputStream()
/*     */       throws IOException
/*     */     {
/* 136 */       return this.manifestBlob.getInputStream();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     public long getPatchSize()
/*     */     {
/* 146 */       return this.patchBlob.getSize();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     public long getManifestSize()
/*     */     {
/* 156 */       return this.manifestBlob.getSize();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public PatchStore(BlobStore blobStore)
/*     */   {
/* 168 */     this.blobStore = blobStore;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public IncomingPatch createIncomingPatch(String accountId)
/*     */     throws IOException, ServiceException
/*     */   {
/* 183 */     IncomingBlob ib = this.blobStore.createIncoming(null);
/*     */     
/* 185 */     IncomingPatch ip = new IncomingPatch(ib, accountId);
/* 186 */     ib.setContext(ip);
/*     */     
/* 188 */     return ip;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public IncomingPatch getIncomingPatch(String resumeId)
/*     */   {
/* 200 */     return (IncomingPatch)this.blobStore.getIncoming(resumeId).getContext();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public StoredPatch acceptPatch(IncomingPatch ip, int fileId, int version)
/*     */     throws IOException, ServiceException
/*     */   {
/* 219 */     if (version > 1) {
/* 220 */       deletePatch(ip.getAccountId(), fileId);
/*     */     }
/*     */     
/* 223 */     IncomingBlob manifestBlob = this.blobStore.createIncoming(null);
/*     */     try
/*     */     {
/* 226 */       ip.getManifest().writeTo(manifestBlob.getAppendingOutputStream());
/*     */     } catch (IOException e) {
/* 228 */       this.blobStore.deleteIncoming(manifestBlob);
/* 229 */       throw e;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 234 */     BlobStore.StoredBlob psb = this.blobStore.store(ip.patchBlob, getStoredPatchId(ip.getAccountId(), fileId, false), version);
/*     */     
/* 236 */     BlobStore.StoredBlob msb = null;
/*     */     try
/*     */     {
/* 239 */       msb = this.blobStore.store(manifestBlob, getStoredPatchId(ip.getAccountId(), fileId, true), version);
/*     */     } catch (IOException e) {
/* 241 */       this.blobStore.delete(psb, version);
/* 242 */       throw e;
/*     */     }
/*     */     
/* 245 */     StoredPatch sp = new StoredPatch(psb, msb, ip.getAccountId(), null);
/* 246 */     psb.setContext(sp);
/*     */     
/* 248 */     return sp;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void rejectPatch(IncomingPatch ip)
/*     */   {
/* 257 */     this.blobStore.deleteIncoming(ip.patchBlob);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public StoredPatch lookupPatch(String accountId, int fileId, int version)
/*     */   {
/* 270 */     BlobStore.StoredBlob psb = this.blobStore.get(getStoredPatchId(accountId, fileId, false), version);
/*     */     
/* 272 */     if (psb == null) {
/* 273 */       return null;
/*     */     }
/*     */     
/* 276 */     return (StoredPatch)psb.getContext();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void deletePatch(String accountId, int fileId)
/*     */   {
/*     */     try
/*     */     {
/* 288 */       BlobStore.StoredBlob psb = this.blobStore.get(getStoredPatchId(accountId, fileId, false));
/* 289 */       this.blobStore.delete(psb);
/*     */     } finally { BlobStore.StoredBlob msb;
/* 291 */       BlobStore.StoredBlob msb = this.blobStore.get(getStoredPatchId(accountId, fileId, true));
/* 292 */       this.blobStore.delete(msb);
/*     */     }
/*     */   }
/*     */   
/*     */   private String getStoredPatchId(String accountId, int fileId, boolean manifest)
/*     */   {
/* 298 */     return accountId + ':' + fileId + (manifest ? 'M' : 'P');
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/octosync/store/PatchStore.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */